/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import fs from 'fs';
import path from 'path';
import { Scene } from '../../ArkAnalyzer/src/Scene';
import { SceneConfig } from '../../ArkAnalyzer/src/Config';
import { describe, it, expect } from '@jest/globals';
import { GraphBuilder } from '../../Ark2Graph/graphBuilder';
import { ArkIfStmt, ArkReturnStmt } from '../../ArkAnalyzer/src/core/base/Stmt';
import { Connection } from '../../Ark2Graph/Client';

describe('测试分支CFG', () => {
    let client: any;
    let graphBuilder: GraphBuilder;
    let CFGCount: any;
    const CFGquery = `.E().has('type', 'cfg').count()`;
    const cypherBatch = 'g';
    
    beforeAll(async () => {
        client = await Connection.getClient();
        const configPath = path.resolve(__dirname, '../test_config/TestIfStmtCFGConfig.json');
        graphBuilder = new GraphBuilder(client, configPath);
        const configfile = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
        const filePath = configfile.fileTestConfigPath;
        let config: SceneConfig = new SceneConfig();
        config.buildFromJson(filePath);
        config.buildFromProjectDir(config.getTargetProjectDirectory());
        let scene = new Scene();
        scene.buildSceneFromProjectDir(config);
        graphBuilder.setScene(scene);
        await graphBuilder.buildCPG();
    });
   
    /*
        测试项：graph的分支语句转换功能
        标题：分支语句的至少应该包含两个分支（正向）
        级别：关键
        预置条件：目标代码分支语句至少包含两个分支
        操作步骤：1.使用测试数据TestIfStmtCFGConfig.json配置文件 
                 2.对该数据的文件运行graphBuilder.build()
        预期结果：1.输出cfg 
                 2.分支节点至少存在两个后继节点
        测试结果：符合预期结果
        */
    it('Verify that the branch statement has at least two successors', async () => {
        CFGCount = await client.submit(cypherBatch + CFGquery);
        expect(CFGCount._items[0]).toBe(17);
        //if (client) {
            await client.submit('g.V().drop()');
            await client.close();
        //}
    });
});
